﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Toenda.Foundation.Utility;

namespace Toenda.Foundation.IO {
	/// <summary>
	/// Class PathExtensions
	/// </summary>
	public static class PathExtensions {
		// <param name="instance">The instance.</param>
		/// <summary>
		/// Gets the special folders.
		/// </summary>
		/// <returns></returns>
		public static Dictionary<Environment.SpecialFolder, string> GetSpecialFolders() {//this Path instance) {
			Dictionary<Environment.SpecialFolder, string> folders = new Dictionary<Environment.SpecialFolder, string>();
			
			folders.Add(Environment.SpecialFolder.ApplicationData, System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
			folders.Add(Environment.SpecialFolder.CommonApplicationData, System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
			folders.Add(Environment.SpecialFolder.CommonProgramFiles, System.Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles));
			folders.Add(Environment.SpecialFolder.Cookies, System.Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
			folders.Add(Environment.SpecialFolder.Desktop, System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
			folders.Add(Environment.SpecialFolder.DesktopDirectory, System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));
			folders.Add(Environment.SpecialFolder.Favorites, System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites));
			folders.Add(Environment.SpecialFolder.History, System.Environment.GetFolderPath(Environment.SpecialFolder.History));
			folders.Add(Environment.SpecialFolder.InternetCache, System.Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));
			folders.Add(Environment.SpecialFolder.LocalApplicationData, System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
			folders.Add(Environment.SpecialFolder.MyComputer, System.Environment.GetFolderPath(Environment.SpecialFolder.MyComputer));
			folders.Add(Environment.SpecialFolder.MyDocuments, System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
			folders.Add(Environment.SpecialFolder.MyMusic, System.Environment.GetFolderPath(Environment.SpecialFolder.MyMusic));
			folders.Add(Environment.SpecialFolder.MyPictures, System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
			folders.Add(Environment.SpecialFolder.Personal, System.Environment.GetFolderPath(Environment.SpecialFolder.Personal));
			folders.Add(Environment.SpecialFolder.ProgramFiles, System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
			folders.Add(Environment.SpecialFolder.Programs, System.Environment.GetFolderPath(Environment.SpecialFolder.Programs));
			folders.Add(Environment.SpecialFolder.Recent, System.Environment.GetFolderPath(Environment.SpecialFolder.Recent));
			folders.Add(Environment.SpecialFolder.SendTo, System.Environment.GetFolderPath(Environment.SpecialFolder.SendTo));
			folders.Add(Environment.SpecialFolder.StartMenu, System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu));
			folders.Add(Environment.SpecialFolder.Startup, System.Environment.GetFolderPath(Environment.SpecialFolder.Startup));
			folders.Add(Environment.SpecialFolder.System, System.Environment.GetFolderPath(Environment.SpecialFolder.System));
			folders.Add(Environment.SpecialFolder.Templates, System.Environment.GetFolderPath(Environment.SpecialFolder.Templates));

			return folders;
		}

		/// <summary>
		/// Gets the full extension.
		/// </summary>
		/// <param name="path">The path.</param>
		/// <returns></returns>
		public static string GetFullExtension(string path) {
			if(path.Contains(".tar.gz")) {
				return ".tar.gz";
			}
			else if(path.Contains(".tar.bz2")) {
				return ".tar.bz2";
			}
			else {
				return Path.GetExtension(path);
			}
		}

		/// <summary>
		/// Gets the file name without full extension.
		/// </summary>
		/// <param name="path">The path.</param>
		/// <returns></returns>
		public static string GetFileNameWithoutFullExtension(string path) {
			if(path.Contains(".tar.gz")) {
				return path.Replace(".tar.gz", "");
			}
			else if(path.Contains(".tar.bz2")) {
				return path.Replace(".tar.bz2", "");
			}
			else {
				return Path.GetFileNameWithoutExtension(path);
			}
		}

		/// <summary>
		/// Converts the umlaut entities.
		/// </summary>
		/// <param name="text">The text.</param>
		/// <returns></returns>
		public static string ConvertUmlautEntities(string text) {
			string fullPath = Path.GetDirectoryName(text);
			text = Path.GetFileName(text);
			string tmp = "";

			foreach(char c in text) {
				if(!c.IsForbiddenCharacter()) {
					tmp += c.ToString();
				}
				else {
					tmp += "_";
				}
			}

			text = tmp;

			//text = text.Replace("ä", "ae");
			//text = text.Replace("Ä", "Ae");
			//text = text.Replace("ö", "oe");
			//text = text.Replace("Ö", "Oe");
			//text = text.Replace("ü", "ue");
			//text = text.Replace("Ü", "Ue");
			//text = text.Replace("ß", "ss");

			//text = text.Replace(' ', '_');
			//text = text.Replace(" ", "_");

			//text = text.Replace("§", "_");
			//text = text.Replace('"', '_');
			//text = text.Replace("'", "_");
			//text = text.Replace("^", "_");
			//text = text.Replace("°", "_");
			//text = text.Replace("$", "_");
			//text = text.Replace("€", "_");
			//text = text.Replace("@", "_");
			//text = text.Replace("%", "_");
			//text = text.Replace("&", "_");
			//text = text.Replace("/", "_");
			//text = text.Replace("=", "_");
			//text = text.Replace("(", "_");
			//text = text.Replace(")", "_");
			//text = text.Replace("?", "_");
			//text = text.Replace("`", "_");
			//text = text.Replace("´", "_");
			//text = text.Replace("*", "_");
			//text = text.Replace("+", "_");
			//text = text.Replace("#", "_");
			//text = text.Replace(";", "_");
			//text = text.Replace(":", "_");
			//text = text.Replace("<", "_");
			//text = text.Replace(">", "_");
			//text = text.Replace("|", "_");
			////text = text.Replace("~", "_");
			//text = text.Replace("}", "_");
			//text = text.Replace("{", "_");
			//text = text.Replace("]", "_");
			//text = text.Replace("[", "_");
			//text = text.Replace("³", "_");
			//text = text.Replace("²", "_");
			//text = text.Replace("!", "_");
			//text = text.Replace(",", "_");

			string fileName = "";

			if(text.Length > 40) {
				string ext = PathExtensions.GetFullExtension(text);
				string name = PathExtensions.GetFileNameWithoutFullExtension(text);

				int length = 40 - ext.Length;

				fileName = name.Substring(0, length) + ext;
			}
			else {
				fileName = text;
			}

			if(!fullPath.IsNullOrTrimmedEmpty()) {
				return Path.GetFullPath(fullPath + "\\" + fileName);
			}
			else {
				return fileName;
			}
		}
	}
}
